package com.ylzinfo.jck.service.impl;

import com.ylzinfo.jck.common.Const;
import com.ylzinfo.jck.dao.Ac01Mapper;
import com.ylzinfo.jck.dao.Ac02AMapper;
import com.ylzinfo.jck.dao.Ac02LogMapper;
import com.ylzinfo.jck.dao.Ac02Mapper;
import com.ylzinfo.jck.domain.Ac01;
import com.ylzinfo.jck.domain.Ac02;
import com.ylzinfo.jck.domain.Ac02A;
import com.ylzinfo.jck.domain.Ac02Log;
import com.ylzinfo.jck.service.Ac02aService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author sunqy
 * @create 2019/11/20
 */
@Service
public class Ac02aServiceImpl implements Ac02aService {

    private Logger logger = LoggerFactory.getLogger(Ac02aServiceImpl.class);

    @Autowired(required = false)
    private Ac02AMapper ac02AMapper;

    @Autowired(required = false)
    private Ac01Mapper ac01Mapper;

    @Autowired(required = false)
    private Ac02Mapper ac02Mapper;

    @Autowired(required = false)
    private Ac02LogMapper ac02LogMapper;

    @Override
    public int addAc02A(Ac02A ac02A) {
        ac02A.setState("0");
        return ac02AMapper.insert(ac02A);
    }

    /**
     * 批量新增ac02
     * @param list
     */
    @Override
    public int addAc02As(List<Ac02A> list) {
        return ac02AMapper.insertForeach(list);
    }

    /**
     * 定时任务，将各业务系统同步过来的ac02a中的数据经过处理同步到ac02中
     * @param ac02A
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void dealAc02aToAc02(Ac02A ac02A) {
        //根据aac002查询ac01并判断是否存在
        Ac01 ac01 = ac01Mapper.selectByAac002(ac02A.getAac002());
        if (ac01 == null) {//如果不存在ac01则处理失败
            ac02A.setState(Const.DealStatusEnum.AC02_FAIL_AC01_NOT_FOUND.getCode());
            ac02A.setMsg(Const.DealStatusEnum.AC02_FAIL_AC01_NOT_FOUND.getMsg());
            return;
        }
        Ac02Log ac02Log = new Ac02Log();
        Ac02 ac02 = new Ac02();
        List<Ac02> ac02List = ac02Mapper.selectByAac001AndAae140(ac01.getAac001(), ac02A.getAae140(), ac02A.getCenterid());//查询该险种信息原本是否存在
        if (!CollectionUtils.isEmpty(ac02List)) {//存在旧数据，更新，直接覆盖掉原来的那一条
            BeanUtils.copyProperties(ac02List.get(0), ac02);
            BigDecimal aaz159 = ac02.getAaz159();
            BeanUtils.copyProperties(ac02A, ac02);
            ac02.setAaz159(aaz159);//这边要重新设置aaz159，不然会变成ac02a里面的aaz159，这个aaz159可能是各个系统过来的aaz159
            ac02.setAac001(ac01.getAac001());//这边要重新设置aac001，不然会变成ac02a里面的aac001，这个aac001可能是各个系统过来的aac001
            ac02Mapper.updateByPrimaryKeySelective(ac02);
        } else {//不存在旧数据，新增
            BeanUtils.copyProperties(ac02A, ac02);
            ac02.setAac001(ac01.getAac001());
            ac02Mapper.insert(ac02);
        }
        BeanUtils.copyProperties(ac02, ac02Log);
        ac02LogMapper.insert(ac02Log);
        ac02A.setState(Const.DealStatusEnum.OK.getCode());
        ac02A.setMsg(Const.DealStatusEnum.OK.getMsg());
    }
}
